package com.card.service.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.card.consts.GiftStatusConsts;
import com.card.entity.manager.UserGift;
import com.card.repository.IUserGiftRepository;

/**
 * ClassName: TaskService <br/>
 * Function: 物流状态任务. <br/>
 * > date: 2018年12月11日 下午5:03:32 <br/>
 * 
 * @author lishuai11
 * @version
 * @since JDK 1.8
 */
@Component
public class TaskService {

	@Autowired
	private IUserGiftRepository userGiftRepository;

	/**
	 * 
	 * run:(物流状态更改暂时处理). <br/>
	 * 
	 * @author lishuai11
	 * @since JDK 1.8
	 */
	@Scheduled(cron = "0 5 0 * * ?")
	public void run() {
		List<UserGift> userGifts = (List<UserGift>) userGiftRepository.findAll();
		if (userGifts != null && !userGifts.isEmpty()) {
			long currentTime = System.currentTimeMillis();
			long poor;
			for (UserGift userGift : userGifts) {
				poor = currentTime - userGift.getCreatedTime();
				if (poor >= 60 * 60 * 1000) {
					userGift.setStatus(GiftStatusConsts.ALREADY_SEND);
					userGiftRepository.save(userGift);
				}
				if (poor >= 48 * 60 * 60 * 1000) {
					userGift.setStatus(GiftStatusConsts.ALREADY_END);
					userGiftRepository.save(userGift);
				}
			}
		}
	}

}
